PROJECT(BlueBerry)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMake/")

INCLUDE(MacroParseArguments)
INCLUDE(MacroConvertSchema)
INCLUDE(MacroOrganizeSources)
INCLUDE(berryPluginHelpers)
INCLUDE(MacroCollectPlugins)
INCLUDE(MacroParseManifest)
INCLUDE(MacroCreatePlugin)
INCLUDE(MacroCreateQtHelp)
INCLUDE(MacroInstallPlugin)

OPTION(BLUEBERRY_INSTALL_RPATH_RELATIVE "Use relative rpath entries when installing" OFF)
MARK_AS_ADVANCED(BLUEBERRY_INSTALL_RPATH_RELATIVE)

IF(BLUEBERRY_INSTALL_RPATH_RELATIVE)
  SET(CMAKE_INSTALL_RPATH ".")
ELSE()
  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/bin")
ENDIF()


IF(MSVC)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250 /wd4275 /wd4251 /wd4503")
ENDIF()

IF (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
ENDIF ()

FIND_PACKAGE(mbilog REQUIRED)
INCLUDE_DIRECTORIES(${mbilog_INCLUDE_DIRS})

OPTION(BLUEBERRY_USE_QT "Use the Qt GUI toolkit" OFF)

IF(NOT DESIRED_QT_VERSION)
  SET(DESIRED_QT_VERSION 4 CACHE STRING "Desired Qt version" FORCE)
  MARK_AS_ADVANCED(DESIRED_QT_VERSION)
ENDIF()

IF(BLUEBERRY_USE_QT AND NOT DESIRED_QT_VERSION EQUAL 4)
  MESSAGE("Attention: Qt4 is required to build the BlueBerry Qt plug-ins.")
ENDIF()

IF(BLUEBERRY_USE_QT AND DESIRED_QT_VERSION EQUAL 4)
  SET(QT_MT_REQUIRED 1)
  SET(BUILD_QT_PLUGINS 1)
  FIND_PACKAGE(Qt REQUIRED)

  IF(QT_QMAKE_CHANGED)
    SET(QT_HELPGENERATOR_EXECUTABLE NOTFOUND)
    SET(QT_COLLECTIONGENERATOR_EXECUTABLE NOTFOUND)
    SET(QT_ASSISTANT_EXECUTABLE NOTFOUND)
  ENDIF()

  FIND_PROGRAM(QT_HELPGENERATOR_EXECUTABLE
    NAMES qhelpgenerator qhelpgenerator-qt4 qhelpgenerator4
    PATHS ${QT_BINARY_DIR}
    NO_DEFAULT_PATH
  )

  FIND_PROGRAM(QT_COLLECTIONGENERATOR_EXECUTABLE
    NAMES qcollectiongenerator qcollectiongenerator-qt4 qcollectiongenerator4
    PATHS ${QT_BINARY_DIR}
    NO_DEFAULT_PATH
  )

  FIND_PROGRAM(QT_ASSISTANT_EXECUTABLE
    NAMES assistant-qt4 assistant4 assistant
    PATHS ${QT_BINARY_DIR}
    NO_DEFAULT_PATH
  )

  OPTION(BLUEBERRY_USE_QT_HELP "Enable support for integrating bundle documentation into Qt Help" ON)

  MARK_AS_ADVANCED(BLUEBERRY_USE_QT_HELP
                   QT_HELPGENERATOR_EXECUTABLE
                   QT_COLLECTIONGENERATOR_EXECUTABLE
                   QT_ASSISTANT_EXECUTABLE)

  SET(_doxygen_too_old 1)
  IF(BLUEBERRY_USE_QT_HELP)
    FIND_PACKAGE(Doxygen)
    IF(DOXYGEN_FOUND)
      EXECUTE_PROCESS(COMMAND ${DOXYGEN_EXECUTABLE} --version
                      OUTPUT_VARIABLE _doxygen_version)
      IF(${_doxygen_version} VERSION_GREATER 1.6.0 OR
         ${_doxygen_version} VERSION_EQUAL 1.6.0)
        SET(_doxygen_too_old 0)
      ENDIF()
    ENDIF()
  ELSE(BLUEBERRY_USE_QT_HELP)
  
	CONFIGURE_FILE( ../Documentation/pregenerated/MITKBlankPage.qch
				${MITK_BINARY_DIR}/bin/ExtBundles/org.mitk.gui.qt.extapplication/resources/MITKBlankPage.qch
				COPYONLY )
				
	CONFIGURE_FILE( ../Documentation/pregenerated/MitkExtQtHelpCollection.qhc
				${MITK_BINARY_DIR}/bin/ExtBundles/org.mitk.gui.qt.extapplication/resources/MitkExtQtHelpCollection.qhc
				COPYONLY )
  
  ENDIF(BLUEBERRY_USE_QT_HELP)

  IF (BLUEBERRY_USE_QT_HELP AND _doxygen_too_old)
    MESSAGE("Doxygen was not found or is too old. Version 1.6.0 or later is needed if BLUEBERRY_USE_QT_HELP is ON")
    SET(BLUEBERRY_USE_QT_HELP OFF CACHE BOOL "Enable support for integrating bundle documentation into Qt Help" FORCE)
  ENDIF()

  IF(BLUEBERRY_USE_QT_HELP AND NOT QT_HELPGENERATOR_EXECUTABLE)
    MESSAGE("You have enabled Qt Help support, but QT_HELPGENERATOR_EXECUTABLE is empty")
    SET(BLUEBERRY_USE_QT_HELP OFF CACHE BOOL "Enable support for integrating bundle documentation into Qt Help" FORCE)
  ENDIF()

  INCLUDE(${QT_USE_FILE})
ENDIF()

OPTION(BLUEBERRY_BUILD_ALL_PLUGINS "Build all BlueBerry plugins (overriding selection)" OFF)
MARK_AS_ADVANCED(BLUEBERRY_BUILD_ALL_PLUGINS)

IF(BLUEBERRY_BUILD_ALL_PLUGINS)
  SET(BLUEBERRY_BUILD_ALL_PLUGINS_OPTION "FORCE_BUILD_ALL")
ENDIF()

OPTION(BLUEBERRY_STATIC "Build all plugins as static libraries" OFF)
MARK_AS_ADVANCED(BLUEBERRY_STATIC)

OPTION(BLUEBERRY_DEBUG_SMARTPOINTER "Enable code for debugging smart pointers" OFF)
MARK_AS_ADVANCED(BLUEBERRY_DEBUG_SMARTPOINTER)

FIND_PACKAGE(Poco REQUIRED)
FIND_PACKAGE(Ant)
FIND_PACKAGE(Eclipse)

SET(BLUEBERRY_SOURCE_DIR ${BlueBerry_SOURCE_DIR})
SET(BLUEBERRY_BINARY_DIR ${BlueBerry_BINARY_DIR})

SET(BLUEBERRY_PLUGINS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Bundles)
SET(BLUEBERRY_PLUGINS_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/Bundles)

SET(OSGI_APP solstice)

# Force should be removed after everybody has configured their old binary tree
SET(BLUEBERRY_PLUGINS_OUTPUT_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/BlueBerry CACHE PATH "Directory where to build the BlueBerry Bundles" FORCE)
MARK_AS_ADVANCED(BLUEBERRY_PLUGINS_OUTPUT_DIR)

SET(BLUEBERRY_TEST_PLUGINS_OUTPUT_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/BlueBerryTests CACHE PATH "Directory where to build the BlueBerry test bundles")
MARK_AS_ADVANCED(BLUEBERRY_TEST_PLUGINS_OUTPUT_DIR)

# Clear the cache variables
SET(BLUEBERRY_PLUGIN_SOURCE_DIRS "" CACHE INTERNAL "List of base plugin source directories" FORCE)
SET(BLUEBERRY_PLUGIN_BINARY_DIRS "" CACHE INTERNAL "List of base plugin binary directories" FORCE)

IF (Eclipse_DIR)
  SET(BLUEBERRY_DOC_TOOLS_DIR "${Eclipse_DIR}" CACHE PATH "Directory containing additional tools needed for generating the documentation")
ELSE ()
  SET(BLUEBERRY_DOC_TOOLS_DIR "" CACHE PATH "Directory containing additional tools needed for generating the documentation")
ENDIF ()

SET(BLUEBERRY_DEBUG_POSTFIX d)

CONFIGURE_FILE(BlueBerryConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/BlueBerryConfig.cmake @ONLY)

ADD_SUBDIRECTORY(Bundles)

ADD_SUBDIRECTORY(Documentation)


# Testing options

OPTION(BLUEBERRY_BUILD_TESTING "Build the BlueBerry tests." OFF)

IF(WIN32)
  SET(_gui_testing_default "ON")
ELSE()
  SET(_gui_testing_default "OFF")
ENDIF()

OPTION(BLUEBERRY_ENABLE_GUI_TESTING "Enable the BlueBerry GUI tests" ${_gui_testing_default})
MARK_AS_ADVANCED(BLUEBERRY_ENABLE_GUI_TESTING)

IF(BLUEBERRY_BUILD_TESTING)
  ENABLE_TESTING()
  ADD_SUBDIRECTORY(Testing)
ENDIF()
